/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import {
  Body,
  Controller,
  Delete,
  Get,
  ParseIntPipe,
  Post,
  Put,
  Query,
  Req,
} from '@nestjs/common';
import { ArticleService } from './article.service';
import { ArticleDto, ArticleUpdateDto } from './dto/article.dto';
import { ResponseData, ResponseDataProps } from 'src/common/response.data';
import { PaginationDto } from './dto/pagination.dto';
import { ApiBearerAuth } from '@nestjs/swagger';

@ApiBearerAuth()
@Controller('article')
export class ArticleController {
  constructor(private readonly articleService: ArticleService) {}

  @Get('list')
  async list(@Query() data: PaginationDto): Promise<ResponseDataProps> {
    return ResponseData.success(await this.articleService.findAll(data));
  }

  @Get()
  async detail(
    @Query('id', ParseIntPipe) id: number,
  ): Promise<ResponseDataProps> {
    return ResponseData.success(await this.articleService.findOneBy({ id }));
  }
  @Post()
  async create(
    @Req() req: any,
    @Body() body: ArticleDto,
  ): Promise<ResponseDataProps> {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
    body.create_user = req.user.userid;
    return ResponseData.success(await this.articleService.create(body));
  }

  @Put()
  async update(@Body() body: ArticleUpdateDto): Promise<ResponseDataProps> {
    return ResponseData.success(await this.articleService.update(body));
  }

  @Delete()
  async delete(
    @Query('id', ParseIntPipe) id: number,
  ): Promise<ResponseDataProps> {
    return ResponseData.success(await this.articleService.delete(id));
  }
}
